/**  
 * Project Name:mmc_svn  
 * File Name:MdmImportImpl.java  
 * Package Name:com.service.hbydMDM.impl  
 * Date:Apr 23, 201712:05:23 AM  
 * Copyright (c) 2017, MM-MES All Rights Reserved.  
*/  
  
package com.mes.integration.hbydMPM.impl;  

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.mes.basicdata.domain.Clamp;
import com.mes.basicdata.domain.Cutter;
import com.mes.basicdata.domain.Dept;
import com.mes.basicdata.domain.FileObject;
import com.mes.basicdata.domain.FileObjectRelation;
import com.mes.basicdata.domain.Measurer;
import com.mes.basicdata.domain.Operation;
import com.mes.basicdata.domain.OperationPart;
import com.mes.basicdata.domain.OperationResource;
import com.mes.basicdata.domain.Part;
import com.mes.basicdata.domain.Routing;
import com.mes.basicdata.domain.XbomDetail;

import com.mes.basicdata.manager.ClampManager;
import com.mes.basicdata.manager.CutterManager;
import com.mes.basicdata.manager.DeptManager;
import com.mes.basicdata.manager.FileObjectManager;
import com.mes.basicdata.manager.FileObjectRelationManager;
import com.mes.basicdata.manager.MeasurerManager;
import com.mes.basicdata.manager.OperationManager;
import com.mes.basicdata.manager.OperationMeasurementManager;
import com.mes.basicdata.manager.OperationPartManager;
import com.mes.basicdata.manager.OperationResourceManager;
import com.mes.basicdata.manager.PartManager;
import com.mes.basicdata.manager.RoutingManager;
import com.mes.basicdata.manager.RoutingOperationManager;
import com.mes.basicdata.manager.ToolManager;
import com.mes.basicdata.manager.XbomDetailManager;

import com.mes.common.util.ConfigInfo;
import com.mes.common.util.IdentityGenerator;
import com.mes.common.util.SysUtil;
import com.mes.integration.hbydMPM.domain.MpmAssistItem;
import com.mes.integration.hbydMPM.domain.MpmBom;
import com.mes.integration.hbydMPM.domain.MpmClamper;
import com.mes.integration.hbydMPM.domain.MpmConsumeItem;
import com.mes.integration.hbydMPM.domain.MpmCutter;
import com.mes.integration.hbydMPM.domain.MpmFile;
import com.mes.integration.hbydMPM.domain.MpmItem;
import com.mes.integration.hbydMPM.domain.MpmItemData;
import com.mes.integration.hbydMPM.domain.MpmMeasurer;
import com.mes.integration.hbydMPM.domain.MpmOperation;
import com.mes.integration.hbydMPM.domain.MpmRouting;
import com.mes.integration.hbydMPM.domain.MpmRoutingData;
import com.mes.integration.hbydMPM.domain.MpmUnit;
import com.mes.integration.hbydMPM.iface.IMpmImportService;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**  
 * ClassName:MpmImportImpl <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     Apr 23, 2017 12:05:23 AM <br/>  
 * @author   Administrator  
 * @version    
 * @since    JDK 1.6  
 * @see        
 */
@Service
public class MpmImportService implements IMpmImportService {

	@Autowired
	private ConfigInfo configInfo;
	
	@Autowired
	private PartManager partManager;
	
	@Autowired
	private RoutingManager routingManager;
	
	@Autowired
	private OperationManager operationManager;
	
	@Autowired
	private RoutingOperationManager routingOperationManager;
	
	@Autowired
	private OperationPartManager operationPartManager;
	
	@Autowired
	private OperationResourceManager operationResourceManager;
	
	@Autowired
	private XbomDetailManager xbomDetailManager;
	
	@Autowired
	private CutterManager cutterManager;
	
	@Autowired
	private ClampManager clampManager;
	
	@Autowired
	private MeasurerManager measurerManager;
	
	@Autowired
	private ToolManager toolManager;
	
	@Autowired
	private FileObjectManager fileObjectManager;
	
	@Autowired
	private FileObjectRelationManager fileObjectRelationManager;
	
	@Autowired
	private OperationMeasurementManager operationMeasurementManager;
	
	@Autowired
	private DeptManager deptManager;
	
	private int count;
	
	private String currentAOOperationIdDesc;

	@Override
	public String importItemData(String itemDataString) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String importRoutingData(String routingDataString) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String importItemDataFile(String itemDataFilePath) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String importItemDataLocalFile(String itemDataFilePath) {
		// TODO Auto-generated method stub
		return null;
	}
	
//	@Override
//	public String importItemData(String itemDataString) {
//		String result = "";
//		count = 0;
//		currentAOOperationIdDesc = "";
//		try {
//			XStream xStream = new XStream(new DomDriver());
//			xStream.processAnnotations(MpmItemData.class);
//			xStream.autodetectAnnotations(true);
//			MpmItemData mpmItemData = (MpmItemData)xStream.fromXML(itemDataString);
//			
//			List<MpmItem> mpmItemList = mpmItemData.getItems();
//			importItems(mpmItemList);
//
//			List<MpmBom> mpmBomList = mpmItemData.getBoms();
//			importBoms(mpmBomList);
//			
//		} catch (Exception e) {
//			e.printStackTrace();
//			result = "已成功导入AO条数： " + count + " 条，从AO编号： " + currentAOOperationIdDesc + " 开始导入失败！";
//			return result;
//		}
//		result = "已成功导入AO条数： " + count + " 条！";
//		return result;
//	}
//
//	private void importBoms(List<MpmBom> mpmBomList) throws Exception {
//		if (!CollectionUtils.isEmpty(mpmBomList)) {
//			for (MpmBom mpmBom : mpmBomList) {
//				XbomDetail xbomDetail = new XbomDetail();
//				xbomDetail.setOutUid(mpmBom.getOutUniqueId());
//				xbomDetail.setIsDelete(mpmBom.getIsDelete());
//				if (mpmBom.getIsDelete()) {
//					xbomDetailManager.sync(xbomDetail);
//					continue;
//				}
//				Routing routing = routingManager.getSingleByOutUid(mpmBom.getRoutingUid());
//				if (routing != null) {
//					xbomDetail.setXbomUid(routing.getRoutingUid());
//				}else{
//					continue;
//				}
////				xbomDetail.setXbomUid(mpmBom.getRoutingUid());
//				
//				Routing subRouting = (Routing)routingManager.getSingleByOutUid(mpmBom.getSubRoutingUid());
//				if (subRouting != null) {
//					xbomDetail.setSubXbomUid(subRouting.getRoutingUid());
//				}
////				xbomDetail.setSubXbomUid(mpmBom.getSubRoutingUid());
//				Part parentPart = (Part) partManager.getSingleByOutUid(mpmBom.getParentItemUid());
//				if (parentPart != null) {
//					xbomDetail.setParentPartUid(parentPart.getPartUid());
//				}else{
//					continue;
//				}
//				Part childPart = (Part) partManager.getSingleByOutUid(mpmBom.getChildItemUid());
//				if (childPart != null) {
//					xbomDetail.setChildPartUid(childPart.getPartUid());
//				}else{
//					continue;
//				}
////				xbomDetail.setParentPartUid(mpmBom.getParentItemUid());
////				xbomDetail.setChildPartUid(mpmBom.getChildItemUid());
////				xbomDetail.setUuid(mpmBom.getOutUniqueId());
//				xbomDetail.setChildQty(1L);
//				xbomDetail.setUuid(IdentityGenerator.GetIdentityUUID());
//				
//				xbomDetailManager.sync(xbomDetail);
//			}
//			
//		}
//	}
//
//	private void importItems(List<MpmItem> mpmItemList) throws Exception {
//		if (!CollectionUtils.isEmpty(mpmItemList)) {
//			for (MpmItem mpmItem : mpmItemList) {
//				Part part = new Part();
//				part.setOutUid(mpmItem.getOutUniqueId());
//				part.setDrawingId(mpmItem.getDrawingId());
//				part.setPartName(mpmItem.getName());
//				if (mpmItem.getPartType() != null) {
//				
//					if (mpmItem.getPartType().equals("8")) {
//						part.setPartType("01");
//					}else if(mpmItem.getPartType().equals("1") || mpmItem.getPartType().equals("2") || mpmItem.getPartType().equals("3")){
//						part.setPartType("02");
//					}else {
//						part.setPartType("03");
//					}
//				
//				}else {
//					part.setPartType(mpmItem.getPartType());
//				}
//				
//				part.setDescription(mpmItem.getSpecification());
//				part.setVersionId(mpmItem.getVersionId());
//				part.setModelClassId(mpmItem.getModel());
//				part.setIsDelete(mpmItem.getIsDelete());
//				part.setProductDeptUid("0009");
//				part.setUuid(IdentityGenerator.GetIdentityUUID());
////				part.setUuid(mpmItem.getOutUniqueId());
//				partManager.sync(part);
//				
//				
//			}
//			for (MpmItem mpmItem : mpmItemList) {
//				List<MpmRouting> mpmRoutingList = mpmItem.getRoutings();
//				importRoutings(mpmRoutingList);
//			}
//			
//		}
//	}
//
//	@Override
//	public String importRoutingData(String routingDataString) {
//		String result = "";
//		count = 0;
//		currentAOOperationIdDesc = "";
//		try{
//			XStream xStream = new XStream(new DomDriver());
//			xStream.processAnnotations(MpmRoutingData.class);
//			MpmRoutingData mpmRoutingData = (MpmRoutingData)xStream.fromXML(routingDataString);
//			
//			List<MpmRouting> mpmRoutingList = mpmRoutingData.getRoutings();
//			importRoutings(mpmRoutingList);
//		} catch (Exception e) {
//			e.printStackTrace();
//			result = "已成功导入AO条数： " + count + " 条，从AO编号： " + currentAOOperationIdDesc + " 开始导入失败！";
//			return result;
//		}
//		result = "已成功导入AO条数： " + count + " 条！";
//		return result;
//			
//	}
//
//	private void importRoutings(List<MpmRouting> mpmRoutingList) throws Exception {
//		if (!CollectionUtils.isEmpty(mpmRoutingList)) {
//			for (MpmRouting mpmRouting : mpmRoutingList) {
//				Routing routing = new Routing();
//				routing.setOutUid(mpmRouting.getOutUniqueId());
//				routing.setIsDelete(mpmRouting.getIsDelete());
//				if (mpmRouting.getIsDelete()) {
//					routingManager.sync(routing);
//					continue;
//				}
//				routing.setRoutingName(mpmRouting.getName());
//				routing.setVersionId(mpmRouting.getVersionId());
//				routing.setOpType(StringUtils.isBlank(mpmRouting.getOptype()) ? 0 : Integer.valueOf(mpmRouting.getOptype()));
////				routing.setMasterShopUid(mpmRouting.getMasterShopId());
//				Part part = (Part) partManager.getSingleByOutUid(mpmRouting.getOutItemUid());
//				if (part != null) {
//					routing.setPartUid(part.getPartUid());
//				}else{
//					continue;
//				}
////				routing.setPartUid(mpmRouting.getOutItemUid());
//				MpmUnit mpmUnit = mpmRouting.getMpmUnit();
//				routing.setMasterShopUid("0009");
//				if (mpmUnit != null) {
//					Dept dept = new Dept();
//					dept.setUuid("611-" + mpmUnit.getDeptId());
//					//dept.setDeptUid(mpmUnit.getDeptId());
//					dept.setDeptName(mpmUnit.getDeptName());
//					dept.setParentDeptUid("00");
//					dept.setOutUid(mpmUnit.getDeptId());
//					deptManager.sync(dept);
//					
//					if("611".equals(mpmUnit.getDeptId())){
//						routing.setMasterShopUid("0009");
//					}else{
//						routing.setMasterShopUid(dept.getUuid());
//					}
//				}
////				routing.setIsDelete(mpmRouting.getIsDelete());
//				routing.setUuid(IdentityGenerator.GetIdentityUUID());
////				routing.setUuid(mpmRouting.getOutUniqueId());
//				routingManager.sync(routing);
//				
//				List<MpmOperation> mpmOperationList = mpmRouting.getOperations();
//				importOperations(routing, part, mpmOperationList);
//				if (routing.getOperations().size() == 0) {
//					if (!part.getPartType().equals("01")) {
//						//routingManager.delete(routing);
//					}
//				}
//			}
//			
//		}
//	}
//
//	private void importOperations(Routing routing, Part part,
//			List<MpmOperation> mpmOperationList) throws Exception {
//		if (!CollectionUtils.isEmpty(mpmOperationList)) {
//			for (MpmOperation mpmOperation : mpmOperationList) {
//				Operation operation = new Operation();
//				
//				routing.getOperations().add(operation);
//				Routing parentRouting = routing.getParentRouting();
//				if (parentRouting != null) {
//					parentRouting.getOperations().add(operation);
//				}
//				
//				
//				operation.setOutUid(mpmOperation.getOutUniqueId());
//				/*if (StringUtils.isBlank(mpmOperation.getRefRoutingUid())) {
//					operation.setOperationId(StringUtils.isBlank(mpmOperation.getOperationId()) ? 0f : Float.valueOf(mpmOperation.getOperationId()));
//				}*/
//				operation.setOperationId(StringUtils.isBlank(mpmOperation.getOperationId()) ? 0f : Float.valueOf(mpmOperation.getOperationId()));
////				operation.setOperationIdDesc(mpmOperation.getOperationIdDesc());
//				operation.setOperationIdDesc(mpmOperation.getOperationIdDesc());
//				operation.setOperationName(mpmOperation.getOperationName());
//				operation.setOperationContent(mpmOperation.getOperationContent());
////				operation.setPreopTime(mpmOperation.getPrepareTime());
////				operation.setRunTime(mpmOperation.getRunTime());
//				operation.setIsNeedCheck(true);
//				
//				// TODO 此处的routingoperation好像没有处理
////				operation.setRoutingUid(routing.getRoutingUid());
////				operation.setRoutingUid(mpmOperation.getOutRoutingUid());
//				operation.setIsDelete(mpmOperation.getIsDelete());
//				if (StringUtils.isNotBlank(mpmOperation.getRefRoutingUid())) {
//					
//					MpmRouting mpmRouting = mpmOperation.getRefRouting();
//					if (mpmRouting != null) {
//						Routing refRouting = new Routing();
//						refRouting.setOutUid(mpmRouting.getOutUniqueId());
//						refRouting.setIsDelete(mpmRouting.getIsDelete());
//						if (mpmRouting.getIsDelete()) {
//							routingManager.sync(refRouting);
//							continue;
//						}
//						refRouting.setRoutingName(mpmRouting.getName());
//						refRouting.setVersionId(mpmRouting.getVersionId());
//						refRouting.setContignationNumber(mpmRouting.getBatchNum());
//						refRouting.setRoutingId(mpmRouting.getRoutingId());
//						
//						refRouting.setOpType(StringUtils.isBlank(mpmRouting.getOptype())? 0 : Integer.valueOf(mpmRouting.getOptype()));
//						refRouting.setParentRoutingUid(routing.getRoutingUid());
//						refRouting.setParentRouting(routing);
//						
//						
//						//refRouting.setMasterShopUid(mpmRouting.getMasterShopId());
//						
//						
//						Part refPart = (Part) partManager.getSingleByOutUid(mpmRouting.getOutItemUid());
//						if (refPart != null) {
//							refRouting.setPartUid(refPart.getPartUid());
//						}else {
//							continue;
//						}
////						refRouting.setPartUid(mpmRouting.getOutItemUid());
//						refRouting.setUuid(IdentityGenerator.GetIdentityUUID());
////						routing.setUuid(mpmRouting.getOutUniqueId());
//						routingManager.sync(refRouting);
//						
//						operation.setRefRoutingUid(refRouting.getRoutingUid());
//						operation.setOperationType(Operation.OPERATION_TYPE_REF);
//						currentAOOperationIdDesc = operation.getOperationIdDesc();
//						
//						List<MpmOperation> mpmOperationList2 = mpmRouting.getOperations();
//						importOperations(refRouting, refPart, mpmOperationList2);
//						if (refRouting.getOperations().size() == 0) {
//							//routingManager.delete(refRouting);
////							operation.setIsDelete(true);
//						}
//					}
//					
//					
//				}
////				operation.setRefRoutingUid(mpmOperation.getRefRoutingUid());
//				
//				
//				operation.setUuid(String.valueOf(IdentityGenerator.GetIdentityLong()));
////				operation.setUuid(mpmOperation.getOutUniqueId());
//				operationManager.sync(operation);
//				if (Operation.OPERATION_TYPE_REF.equals(operation.getOperationType())) {
//					count++;
//				}
//				
//				List<MpmConsumeItem> consumeItems = mpmOperation.getConsumeItems();
//				importConsumeItems(routing, operation, part, consumeItems);
//				
//				
//				
//				List<MpmCutter> cutters = mpmOperation.getCutters();
//				if (!CollectionUtils.isEmpty(cutters)) {
//					for (MpmCutter mpmCutter : cutters) {
//						Cutter cutter = new Cutter();
//						cutter.setOutUid(mpmCutter.getOutUniqueId());
//						cutter.setBarcode(mpmCutter.getCode());
//						cutter.setToolName(mpmCutter.getName());
//						cutter.setToolSpecification(mpmCutter.getSpecification());
//						cutter.setToolTypeUid("0601");
////						cutter.setIsDelete(mpmCutter.getIsDelete());
////						cutter.setUuid(mpmCutter.getOutUniqueId());
//						String uuid = IdentityGenerator.GetIdentityUUID();
//						cutter.setUuid(uuid);
//						//cutter.setToolUid(uuid);
//						cutterManager.sync(cutter);
//						
//						OperationResource operationResource = new OperationResource();
//						operationResource.setOutUid(mpmCutter.getOutOperationRelationUid());
//						operationResource.setIsDelete(mpmCutter.getIsDelete());
//						operationResource.setOperationUid(operation.getUuid());
//						Cutter tool = (Cutter) cutterManager.getSingleByOutUid(mpmCutter.getOutUniqueId());
//						if (tool != null) {
//							operationResource.setResourceUid(tool.getToolUid());
//						}else{
//							continue;
//						}
////						operationResource.setResourceUid(mpmOpResource.getOutResourceUid());
////						operationResource.setOperationUid(mpmOpResource.getOutOperationUid());
////						operationResource.setIsDelete(mpmCutter.getIsDelete());
////						operationResource.setUuid(mpmOpResource.getOutUniqueId());
//						operationResource.setUuid(IdentityGenerator.GetIdentityUUID());
//						operationResourceManager.sync(operationResource);
//
//					}
//					
//				}
//				
//				List<MpmClamper> clampers = mpmOperation.getClampers();
//				if (!CollectionUtils.isEmpty(clampers)) {
//					for (MpmClamper mpmClamper : clampers) {
//						Clamp clamp = new Clamp();
//						clamp.setOutUid(mpmClamper.getOutUniqueId());
//						clamp.setBarcode(mpmClamper.getCode());
//						clamp.setToolName(mpmClamper.getName());
//						clamp.setToolSpecification(mpmClamper.getSpecification());
//						clamp.setToolTypeUid("0604");
////						clamp.setIsDelete(mpmClamper.getIsDelete());
////						clamp.setUuid(mpmClamper.getOutUniqueId());
//						String uuid = IdentityGenerator.GetIdentityUUID();
//						clamp.setUuid(uuid);
//						//clamp.setToolUid(uuid);
//						clampManager.sync(clamp);
//
//						
//						OperationResource operationResource = new OperationResource();
//						operationResource.setOutUid(mpmClamper.getOutOperationRelationUid());
//						operationResource.setOperationUid(operation.getUuid());
//						Clamp tool = (Clamp) clampManager.getSingleByOutUid(mpmClamper.getOutUniqueId());
//						if (tool != null) {
//							operationResource.setResourceUid(tool.getToolUid());
//						}else{
//							continue;
//						}
////						operationResource.setResourceUid(mpmOpResource.getOutResourceUid());
////						operationResource.setOperationUid(mpmOpResource.getOutOperationUid());
//						operationResource.setIsDelete(mpmClamper.getIsDelete());
////						operationResource.setUuid(mpmOpResource.getOutUniqueId());
//						operationResource.setUuid(IdentityGenerator.GetIdentityUUID());
//						operationResourceManager.sync(operationResource);
//					}
//					
//				}
//				
//				List<MpmMeasurer> mpmMeasurers = mpmOperation.getMeasurers();
//				if (!CollectionUtils.isEmpty(mpmMeasurers)) {
//					for (MpmMeasurer mpmMeasurer : mpmMeasurers) {
//						Measurer measurer = new Measurer();
////						measurer.setOutUid(mpmMeasurer.getOutUniqueId());
//						measurer.setOutUid(mpmMeasurer.getOutUniqueId());
//						measurer.setBarcode(mpmMeasurer.getCode());
//						measurer.setToolName(mpmMeasurer.getName());
//						measurer.setToolSpecification(mpmMeasurer.getSpecification());
//						measurer.setToolTypeUid("0603");
////						measurer.setIsDelete(mpmMeasurer.getIsDelete());
////						measurer.setUuid(mpmMeasurer.getOutUniqueId());
//						String uuid = IdentityGenerator.GetIdentityUUID();
//						measurer.setUuid(uuid);
//						//measurer.setToolUid(uuid);
//						measurerManager.sync(measurer);
//						
//						OperationResource operationResource = new OperationResource();
//						operationResource.setOutUid(mpmMeasurer.getOutOperationRelationUid());
//						operationResource.setOperationUid(operation.getUuid());
//						Measurer tool = (Measurer) measurerManager.getSingleByOutUid(mpmMeasurer.getOutUniqueId());
//						if (tool != null) {
//							operationResource.setResourceUid(tool.getToolUid());
//						}else{
//							continue;
//						}
////						operationResource.setResourceUid(mpmOpResource.getOutResourceUid());
////						operationResource.setOperationUid(mpmOpResource.getOutOperationUid());
//						operationResource.setIsDelete(mpmMeasurer.getIsDelete());
////						operationResource.setUuid(mpmOpResource.getOutUniqueId());
//						operationResource.setUuid(IdentityGenerator.GetIdentityUUID());
//						operationResourceManager.sync(operationResource);
//
//					}
//					
//				}
//				
//				List<MpmAssistItem> mpmAssistItems = mpmOperation.getAssistItems();
//				if (!CollectionUtils.isEmpty(mpmAssistItems)) {
//					for (MpmAssistItem mpmAssistItem : mpmAssistItems) {
//						Part assistPart = new Part();
////						assistPart.setOutUid(mpmAssistItem.getOutUniqueId());
//						assistPart.setOutUid(mpmAssistItem.getOutUniqueId());
//						assistPart.setBarCode(mpmAssistItem.getCode());
//						assistPart.setPartName(mpmAssistItem.getName());
//						assistPart.setDescription(mpmAssistItem.getSpecification());
//						assistPart.setPartType("09");
////						assistPart.setIsDelete(mpmAssistItem.getIsDelete());
////						assistPart.setUuid(mpmAssistItem.getOutUniqueId());
//						assistPart.setUuid(IdentityGenerator.GetIdentityUUID());
//						partManager.sync(assistPart);
//						
//						OperationResource operationResource = new OperationResource();
//						operationResource.setOutUid(mpmAssistItem.getOutOperationRelationUid());
//						operationResource.setOperationUid(operation.getUuid());
//						Part tool = (Part) partManager.getSingleByOutUid(mpmAssistItem.getOutUniqueId());
//						if (tool != null) {
//							operationResource.setResourceUid(tool.getPartUid());
//						}else {
//							continue;
//						}
////						operationResource.setResourceUid(mpmAssistItem.getOutResourceUid());
////						operationResource.setOperationUid(mpmAssistItem.getOutOperationUid());
//						operationResource.setIsDelete(mpmAssistItem.getIsDelete());
////						operationResource.setUuid(mpmAssistItem.getOutUniqueId());
//						operationResource.setUuid(IdentityGenerator.GetIdentityUUID());
//						operationResourceManager.sync(operationResource);
//
//					}
//					
//				}
//				
//				List<MpmFile> mpmFiles = mpmOperation.getFiles();
//				if (!CollectionUtils.isEmpty(mpmFiles)) {
//					for (MpmFile mpmFile : mpmFiles) {
//						FileObject fileObject = new FileObject();
//						fileObject.setFileName(mpmFile.getName());
//						fileObject.setFileType(mpmFile.getType());
//						fileObject.setFilePath(mpmFile.getPath());
//						fileObject.setOutUid(mpmFile.getOutUniqueId());
////						fileObject.setIsDelete(mpmFile.getIsDelete());
//						fileObject.setUuid(IdentityGenerator.GetIdentityUUID());
////						fileObject.setUuid(mpmFile.getOutUniqueId());
//						fileObjectManager.sync(fileObject);
//
//						FileObjectRelation fileField = new FileObjectRelation();
//						fileField.setOutUid(mpmFile.getOutOperationRelationUid());
//						fileField.setObjectType("OPERATION_FILE");
//						fileField.setObjectUid(operation.getUuid());
//						fileField.setFileUid(fileObject.getFileUid());
////						fileField.setFileUid(mpmOpFile.getOutFileUid());
////						fileField.setFieldUid(mpmOpFile.getOutFieldUid());
//						fileField.setIsDelete(mpmFile.getIsDelete());
////						fileField.setUuid(mpmOpFile.getOutUniqueId());
//						fileField.setUuid(IdentityGenerator.GetIdentityUUID());
//						fileObjectRelationManager.sync(fileField);
//					}
//					
//				}
//					
//				
////				List<MpmOpMeasurement> opMeasurements = mpmOperation.getOpMeasurements();
////				if (!CollectionUtils.isEmpty(opMeasurements)) {
////					for (MpmOpMeasurement mpmOpMeasurement : opMeasurements) {
////						OperationMeasurement operationMeasurement = new OperationMeasurement();
////						operationMeasurement.setOutUid(mpmOpMeasurement.getOutUniqueId());
////						operationMeasurement.setCheckInfoType(mpmOpMeasurement.getType());
////						operationMeasurement.setUbound(mpmOpMeasurement.getUBound());
////						operationMeasurement.setLbound(mpmOpMeasurement.getLBound());
////						operationMeasurement.setMeasureBase(mpmOpMeasurement.getMeasureBase());
////						operationMeasurement.setUom(mpmOpMeasurement.getUom());
////						
////						Operation measurementOperation = (Operation) operationManager.getSingleByOutUid(operationMeasurement.getOperationUid());
////						if (measurementOperation != null) {
////							operationMeasurement.setOperationUid(measurementOperation.getOperationUid());
////						}
////						operationMeasurement.setOperationUid(mpmOpMeasurement.getOutOperationUid());
////						operationMeasurement.setIsDelete(mpmOpMeasurement.getIsDelete());
//////						operationMeasurement.setUuid(mpmOpMeasurement.getOutUniqueId());
////						operationMeasurement.setUuid(IdentityGenerator.GetIdentityUUID());
////						operationMeasurementManager.sync(operationMeasurement);
////					}
////				}
//				
//			}
//			
//		}
//	}
//
//	private void importConsumeItems(Routing routing, Operation operation, Part part,
//			List<MpmConsumeItem> consumeItems) throws Exception {
//		if (!CollectionUtils.isEmpty(consumeItems)) {
//			for (MpmConsumeItem mpmConsumeItem : consumeItems) {
//				OperationPart operationPart = new OperationPart();
//				operationPart.setOutUid(mpmConsumeItem.getOutUniqueId());
//				Operation opartOperation = (Operation) operationManager.getSingleByOutUid(mpmConsumeItem.getOutOperationUid());
//				
//				if (mpmConsumeItem.getIsDelete()) {
//					operationPartManager.sync(operationPart);
//					continue;
//				}
//				
//				if (opartOperation != null) {
//					operationPart.setOperationUid(opartOperation.getUuid());
//				}else {
//					operationPart.setOperationUid(operation.getUuid());
//				}
////				operationPart.setOperationUid(mpmConsumeItem.getOperationUid());
////				operationPart.setConsumedPartUid(mpmConsumeItem.getConsumeOutItemUid());
//				Part consumePart = (Part) partManager.getSingleByOutUid(mpmConsumeItem.getConsumeOutItemUid());
//				if (part != null && consumePart != null) {
//					operationPart.setConsumedPartUid(consumePart.getPartUid());
//				}else {
//					continue;
//				}
//				operationPart.setProducedPartUid(routing.getPartUid());
//				operationPart.setConsumedQuantity(mpmConsumeItem.getConsumeQty());
//				operationPart.setProducedQuantity(1d);
//				operationPart.setIsDelete(mpmConsumeItem.getIsDelete());
////				operationPart.setUuid(mpmConsumeItem.getOutUniqueId());
//				operationPart.setUuid(IdentityGenerator.GetIdentityUUID());
//				operationPartManager.sync(operationPart);
//			}
//		}
//	}
//
//	
//	@Override
//	public String importItemDataFile(String itemDataFilePath) {
//		String result = "";
//		if (StringUtils.isNotBlank(itemDataFilePath)) {
//			try {
//				String localFilePath = this.downFile(itemDataFilePath);
//				SAXReader saxReader = new SAXReader();
//				Document document = saxReader.read(new File(localFilePath));
//				String docString = document.asXML();
//				result = this.importItemData(docString);
//				
//				this.deleteFile(itemDataFilePath);
//				
//				return result;
//			} catch (Exception e) {
//				e.printStackTrace();
//				return result;
//			}
//			
//		}
//		return result;
//	}
//	
//	@Override
//	public String importItemDataLocalFile(String itemDataFilePath) {
//		
//		if (StringUtils.isNotBlank(itemDataFilePath)) {
//			try {
//				SAXReader saxReader = new SAXReader();
//				Document document = saxReader.read(new File(itemDataFilePath));
//				String docString = document.asXML();
//				String flag = this.importItemData(docString);
//				
//				return flag;
//			} catch (Exception e) {
//				e.printStackTrace();
//				
//			}
//			
//		}
//		return "导入失败";
//	}
//	
//	public String downFile(String remoteFilePath) {  
//		// ftp登录用户名  
//        String userName = configInfo.getMpmFtpUserName(); //MMUtils.getProperty("mpm.ftpUserName");  
//        // ftp登录密码  
//        String userPassword = configInfo.getMpmFtpPassword(); //MMUtils.getProperty("mpm.ftpPassword");
//        
//        String[] ftpPathStrings = remoteFilePath.split("//");
//        String pathBody = ftpPathStrings[1];
//        String[] ftpDirStrings = pathBody.split("/");
//        // ftp地址:直接IP地址  
//        String server = ftpDirStrings[0];  
//        // 创建的文件  
//        String fileName = ftpDirStrings[ftpDirStrings.length - 1];  
//        // 指定写入的目录  
//        String path = pathBody.substring(pathBody.indexOf("/"), pathBody.lastIndexOf("/") + 1);  
//        // 指定本地写入文件  
//        String localPath="D:\\";  
//          
//        FTPClient ftp = new FTPClient(); 
//        try {  
//            int reply;  
//            //1.连接服务器  
//            ftp.connect(server);  
//            //2.登录服务器 如果采用默认端口，可以使用ftp.connect(url)的方式直接连接FTP服务器  
//            ftp.login(userName, userPassword);  
//            //3.判断登陆是否成功  
//            reply = ftp.getReplyCode();  
//            if (!FTPReply.isPositiveCompletion(reply)) {  
//                ftp.disconnect();  
//            }  
//            //4.指定要下载的目录  
//            ftp.changeWorkingDirectory(path);// 转移到FTP服务器目录  
//            //5.遍历下载的目录  
//            FTPFile[] fs = ftp.listFiles();  
//            for (FTPFile ff : fs) {  
//                //解决中文乱码问题，两次解码  
//                byte[] bytes=ff.getName().getBytes("iso-8859-1");  
//                String fn=new String(bytes,"GBK");  
//                if (fn.equals(fileName)) {  
//                    //6.写操作，将其写入到本地文件中  
//                    File localFile = new File(localPath + fileName);  
//                    OutputStream is = new FileOutputStream(localFile);  
//                    ftp.retrieveFile(ff.getName(), is);  
//                    is.close();  
//                }  
//            }  
//            ftp.logout();  
//        } catch (IOException e) {  
//            e.printStackTrace();  
//        } finally {  
//            if (ftp.isConnected()) {  
//                try {  
//                    ftp.disconnect();  
//                } catch (IOException ioe) {  
//                }  
//            }  
//        }
//		return localPath + fileName;  
//    }  
//	
//	public String deleteFile(String remoteFilePath) {  
//        // ftp登录用户名  
//        String userName = configInfo.getMpmFtpUserName(); //MMUtils.getProperty("mpm.ftpUserName");  
//        // ftp登录密码  
//        String userPassword = configInfo.getMpmFtpPassword(); //MMUtils.getProperty("mpm.ftpPassword");
//        
//        String[] ftpPathStrings = remoteFilePath.split("//");
//        String pathBody = ftpPathStrings[1];
//        String[] ftpDirStrings = pathBody.split("/");
//        // ftp地址:直接IP地址  
//        String server = ftpDirStrings[0];  
//        // 创建的文件  
//        String fileName = ftpDirStrings[ftpDirStrings.length - 1];  
//        // 指定写入的目录  
//        String path = pathBody.substring(pathBody.indexOf("/"), pathBody.lastIndexOf("/") + 1);  
//        // 指定本地写入文件  
//        String localPath="D:\\";  
//          
//        FTPClient ftp = new FTPClient(); 
//        boolean flag = false;
//        try {  
//            int reply;  
//            //1.连接服务器  
//            ftp.connect(server);  
//            //2.登录服务器 如果采用默认端口，可以使用ftp.connect(url)的方式直接连接FTP服务器  
//            ftp.login(userName, userPassword);  
//            //3.判断登陆是否成功  
//            reply = ftp.getReplyCode();  
//            if (!FTPReply.isPositiveCompletion(reply)) {  
//                ftp.disconnect();  
//            }  
//            //4.指定要下载的目录  
//            ftp.changeWorkingDirectory(path);// 转移到FTP服务器目录  
//            //5.遍历下载的目录  
//            FTPFile[] fs = ftp.listFiles(); 
//           
//            for (FTPFile ff : fs) {  
//                //解决中文乱码问题，两次解码  
//                byte[] bytes=ff.getName().getBytes("iso-8859-1");  
//                String fn=new String(bytes,"GBK");  
//                if (fn.equals(fileName)) {  
//                	ftp.deleteFile(fileName);
//                	flag = true;
//                	break;
//                }
//            }  
//            ftp.logout();  
//        } catch (IOException e) {  
//            e.printStackTrace();
//            return "删除失败，文件路径不对或者网络中断！";  
//        } finally {  
//            if (ftp.isConnected()) {  
//                try {  
//                    ftp.disconnect();  
//                } catch (IOException ioe) {  
//                }  
//            }  
//        }
//        if (flag) {
//			return "删除成功！";  
//		}else {
//			return "未找到文件！";  
//		}
//    }
//	
//	public static void main(String[] args) {
//		MpmImportService m = new MpmImportService();
//		try {
//			m.downFile("ftp://192.168.2.156/itemdata/中国航空工业集团公司成都飞机设计研究所制造执行管理系统补充技术协议V4.doc");
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
}
  
